/*
 * Version.java
 *
 * Created on 15 de abril de 2007, 17:46
 */

package servidor;


import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Pellizard
 */
public class Version {
    
    private int mayor;
    private int menor;
    private int revision;
    
    public Version()
    {
        this( 1,0,0 );
    }
    
    /** Creates a new instance of Version */
    public Version( int mayor, int menor, int revision ) {
        
        this.mayor = mayor;
        this.menor = menor;
        this.revision = revision;
    }
    
    public static Version fromString(String version) throws java.lang.ExceptionInInitializerError{
        version.replace( " ", "" );
        String[] p = version.split( "\\." );
        
        if( p.length >= 3 )
        {
            try
            {
                int mayor = Integer.parseInt( p[ 0 ] );
                int menor = Integer.parseInt( p[ 1 ] );
                int revision = Integer.parseInt( p[ 2 ] );
                
                return new Version( mayor,menor,revision );
            }
            catch( Exception e )
            {
                throw new java.lang.ExceptionInInitializerError( "Invalid string format" );
            }
        }
        else if( p.length == 2 )
        {
            try
            {
                int mayor = Integer.parseInt( p[ 0 ] );
                int menor = Integer.parseInt( p[ 1 ] );
                
                return new Version( mayor,menor,0 );
            }
            catch( Exception e )
            {
                throw new java.lang.ExceptionInInitializerError( "Invalid string format" );
            }
        }
        else if( p.length == 1 )
        {
            try
            {
                int mayor = Integer.parseInt( p[ 0 ] );
                
                return new Version( mayor,0,0 );
            }
            catch( Exception e )
            {
                throw new java.lang.ExceptionInInitializerError( "Invalid string format" );
            }
        }
        else
        {
            throw new java.lang.ExceptionInInitializerError( "Invalid string format" );
        }
    }

    public int getMayor() {
        return mayor;
    }

    public void setMayor(int mayor) {
        this.mayor = mayor;
    }

    public int getMenor() {
        return menor;
    }

    public void setMenor(int menor) {
        this.menor = menor;
    }

    public int getRevision() {
        return revision;
    }

    public void setRevision(int revision) {
        this.revision = revision;
    }
    
    public int compareTo ( Object o ){
        
        Version v = ( Version )o;
        
        if ( this.mayor > v.getMayor() )
            return 1;
        else if ( this.mayor < v.getMayor() )
            return -1;
        
        if ( this.menor > v.getMenor() )
            return 1;
        else if ( this.menor < v.getMenor() )
            return -1;
        
        if ( this.revision > v.getRevision() )
            return 1;
        else if ( this.revision < v.getRevision() )
            return -1;
        return 0;
    }
    
    public Node toXml(){
      try
      {
          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          DocumentBuilder parser = fact.newDocumentBuilder();
          Document doc = parser.newDocument();

          return toXml( doc );
      }
      catch( Exception ex )
      {
         return null;
      }
  }
    
    public Node toXml( Document doc ){
        
      try {
      Node root = doc.createElement("version");
      //doc.appendChild(root);

      Node mayor = doc.createElement("mayor");
      mayor.appendChild(doc.createTextNode(String.valueOf(this.mayor)));
      root.appendChild(mayor);
      
      Node menor = doc.createElement("menor");
      menor.appendChild(doc.createTextNode(String.valueOf(this.menor)));
      root.appendChild(menor);
      
      Node revision = doc.createElement("revision");
      revision.appendChild(doc.createTextNode(String.valueOf(this.revision)));
      root.appendChild(revision);
      
      return root;

    } catch (Exception ex) {
      System.err.println("+============================+");
      System.err.println("|        XML Error           |");
      System.err.println("+============================+");
      System.err.println(ex.getClass());
      System.err.println(ex.getMessage());
      System.err.println("+============================+");
      ex.printStackTrace();
      return null;
    }
  } 
    
    public static Version fromXml( String str )
    {
        ByteArrayInputStream is = new ByteArrayInputStream( str.getBytes() );
        
        try
        {
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = fact.newDocumentBuilder();
            Document doc = parser.parse( is );
            
            return fromXml( doc );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
    }
    
    public static Version fromXml( Document doc )
    {
        Version v = new Version();
        
        int mayor = 0, menor = 0, revision = 0;
        
        try
        {
            NodeList nodes = doc.getElementsByTagName( "mayor" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                mayor = Integer.parseInt( node.getFirstChild().getNodeValue() );
            }
            
            nodes = doc.getElementsByTagName( "menor" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                menor = Integer.parseInt( node.getFirstChild().getNodeValue() );
            }
            
            nodes = doc.getElementsByTagName( "revision" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                revision = Integer.parseInt( node.getFirstChild().getNodeValue() );
            }
            
            v.setMayor( mayor );
            v.setMenor( menor );
            v.setRevision( revision );
            
            return v;
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
    }

    
    public String toString(){
        
        return String.valueOf(this.mayor)+ "." +  String.valueOf(this.menor)+ "." + String.valueOf(this.revision);
    }
}
